package org.example.gradle.sharding.jdbc.config;


import org.apache.shardingsphere.sharding.api.sharding.standard.PreciseShardingValue;
import org.apache.shardingsphere.sharding.api.sharding.standard.RangeShardingValue;
import org.apache.shardingsphere.sharding.api.sharding.standard.StandardShardingAlgorithm;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Properties;

/**
 * 标准单键
 */
public class StandardAlgorithmDemo implements StandardShardingAlgorithm<Long> {

    @Override
    public String doSharding(Collection<String> availableTargetNames, PreciseShardingValue<Long> shardingValue) {
//        ShardingValue value = shardingValue.getValue();
//        int shardingCount = availableTargetNames.size();
        List<String> tables = new ArrayList<>(availableTargetNames);
        Long l = shardingValue.getValue() % 2;
        String s = tables.get(l.intValue());
        return s;
    }

    @Override
    public Collection<String> doSharding(Collection<String> availableTargetNames, RangeShardingValue<Long> shardingValue) {
        return availableTargetNames;
    }

    @Override
    public String getType() {
        return null;
    }

    @Override
    public void init(Properties props) {

    }

}
